<template>
	<view class="my-tabBar-wrap">
		<view class="tabBar-item" v-for="(item, index) in tdata" :key="item.id">
			<my-button @click="clickHandel(item, index)">
				<view class="btn-content">
					<text :class="['icon','iconfont',item.icon, item.show ? 'activeIcon' : 'defaultIcon']"></text>
					<view :class="['btn-text',item.show ? 'show' : 'hide']">{{ item.title }}</view>
				</view>
			</my-button>
		</view>
	</view>
</template>

<script>
	export default{
		props:{
			list: {
				type: Array,
				default: () => [],
			},
			
			current: {
				type: Number,
				default: null,
			},
		},
		watch:{
			list(n, o){
				this.tdata = n.map(val => {
					val.show = false;
					return val;
				})
			},
			current(n, o){
				this.tdata = this.dataHandel(this.tdata, n);
				uni.$emit('myTabBarOnChange', n);
			},
		},
		mounted(){
			this.tdata = this.dataHandel(this.list, this.current);
		},
		data(){
			return{
				tdata: [],
			}
		},
		
		methods:{
			clickHandel(item, index){
				this.tdata = this.dataHandel(this.tdata, index);
				uni.$emit('myTabBarOnChange', index);
			},
			
			dataHandel(data, current){
				let newArr = data.map((val, idx) => {
					if(current != idx){
						val.show = false;
					}else{
						val.show = true;
					}
					return val;
				})
				return newArr;
			}
		}
	}
</script>

<style lang="scss" scoped>
.my-tabBar-wrap{
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 40rpx;
	.tabBar-item{
		.btn-content{
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 10rpx 26rpx;
			.defaultIcon{
				font-size: 45rpx;
				color: $my-app-default-color;
			}
			
			.activeIcon{
				font-size: 45rpx;
				color: $my-app-primary-color;
			}
			
			.btn-text{
				display: block;
				width: 160rpx;
				font-size: 26rpx;
				margin-left: 14rpx;
				text-align: left;
				letter-spacing: 2rpx;
				transition: all .5s ease-in-out;
			}
			.show{
				display: block;
				width: 160rpx;
				color: $my-app-primary-color;
				font-weight: bold;
			}
			.hide{
				display: block;
				overflow: hidden;
				width: 0;
				margin: 0;
			}
		}	
	}
}
</style>
